package co.edu.javeriana.touresbalon.oms.webservices.clients.cacheb2c;

import co.edu.javeriana.touresbalon.b2c.entities.Espectaculo;
import co.edu.javeriana.touresbalon.oms.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

/**
 *
 * @author ada
 */
public class CacheB2CClient {

    public void loadCacheB2C(List<Espectaculo> shows) {
        ShowsAdministration showsAdministration = new ShowsAdministration();
        IShowsAdministration iShowsAdministration = showsAdministration.getBasicHttpEndpoint();        
        iShowsAdministration.addShows(buildArrayOfShows(shows));
    }

    public void clearCacheB2C() {
        ShowsAdministration showsAdministration = new ShowsAdministration();
        IShowsAdministration iShowsAdministration = showsAdministration.getBasicHttpEndpoint();
        iShowsAdministration.clearAllShows();
    }

    private ArrayOfShow buildArrayOfShows(List<Espectaculo> shows) {
        ArrayOfShow arrayOfShow=new ArrayOfShow();
        List<Show> showsForCache=new ArrayList<Show>();
        
        for (Espectaculo show : shows) {
            Show showForCache=new Show();
            showForCache.setCapacidad(show.getCapacidad());
            showForCache.setCiudad(new JAXBElement<String>(new QName("ciudad"), String.class, show.getCiudad()) );
            showForCache.setDescripcion(new JAXBElement<String>(new QName("descripcion"), String.class, show.getDescripcionEspectaculo()) );
            showForCache.setFechaFin(Utils.dateToXmlGregorianCalendar(show.getFechaFin()));
            showForCache.setFechaInicio(Utils.dateToXmlGregorianCalendar(show.getFechaInicio()));
            showForCache.setId(new JAXBElement<String>(new QName("id"), String.class, show.getIdEspectaculo()+""));
            showForCache.setNombre(new JAXBElement<String>(new QName("nombre"), String.class, show.getNombreEspectaculo()));
            showForCache.setValor(show.getValorEspectaculo().intValue());
            showsForCache.add(showForCache);
        }
        arrayOfShow.setShow(showsForCache);
        return arrayOfShow;
    }
}
